package com.miracle.java2110.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.miracle.java2110.QiniuUtils2;
import com.miracle.java2110.common.PageVo;
import com.miracle.java2110.common.R;

import com.miracle.java2110.listener.WebEmployeeListener;
import com.miracle.java2110.pojo.Employeemanager;
import com.miracle.java2110.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author miracle
 * @date 2021/11/8 20:39
 * @Description
 */
@RestController
@RequestMapping("/employee")
@CrossOrigin("*")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private WebEmployeeListener webEmployeeListener;


    @RequestMapping("/pageData")
    public PageVo<Employeemanager> getPageData(@RequestParam Map<String, Object> params) {

        return employeeService.getPageData(params);
    }

    @RequestMapping("/delete")
    public R deleteById(Integer id) {
        return employeeService.deleteById(id);
    }

    @RequestMapping("/allDelete")
    public R allDelete(HttpServletRequest request) {
        String id = request.getParameter("id");
        String[] split = id.split(",");
        R r1 = null;
        for (int i = 0; i < split.length; i++) {
            int i1 = Integer.parseInt(split[i]);
            r1 = employeeService.deleteById(i1);
        }
        return r1;
    }

    @RequestMapping("/update")
    public R updateById(Employeemanager employeemanager) {

        return employeeService.updateById(employeemanager);
    }

    @RequestMapping("/addEmployee")
    public R addEmployee(Employeemanager employeemanager) {
        return employeeService.addEmployee(employeemanager);
    }

    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file, HttpServletRequest request) throws IOException {
        String filename = file.getOriginalFilename();

        String suffix = filename.substring(filename.lastIndexOf("."));

        String fileName = UUID.randomUUID().toString() + suffix;
        byte[] bytes = file.getBytes();
        QiniuUtils2.uploadBytes2Qiniu(bytes, fileName);
        return R.ok(200, "上传成功", "http://r23epffak.hn-bkt.clouddn.com/" + fileName);
    }

    @RequestMapping("/read")
    @ResponseBody
    public R readExcel(MultipartFile file) throws IOException {
        ExcelReaderBuilder builder = EasyExcel.read(file.getInputStream(), Employeemanager.class, webEmployeeListener);

        ExcelReaderSheetBuilder sheet = builder.sheet();

        sheet.doRead();
        return R.ok(200, "上传成功");
    }

    @RequestMapping("/write")
    @ResponseBody
    public void writeExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        String filename = URLEncoder.encode("测试文件名", "utf-8");
        response.setHeader("content-Disposition", "attachment;filename=" + filename + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, Employeemanager.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        List<Employeemanager> employeemanagers = employeeService.selectAllEmployeemanager();

        sheet.doWrite(employeemanagers);

    }


}
